package model;

import java.util.Vector;

/**
 * The {@link WindowManager} class is a facade for the view.
 *
 */
public class WindowManager {
	
	private static WindowManager theWindowManager;
	public static WindowManager getTheWindowManager(){
		if (theWindowManager == null) theWindowManager = new WindowManager();
		return theWindowManager;
	}
	
	private Vector<Window> windowStack;

	public WindowManager(){
		this.setWindowStack(new Vector<Window>());
	}
	private void setWindowStack(final Vector<Window> windowStack) {
		this.windowStack = windowStack;
	}
	public Vector<Window> getWindowStack() {
		return windowStack;
	}
	/**
	 * Provides all visible {@link RectangularPart}s of the open windows.
	 * @return all visible {@link RectangularPart}s of the open windows.
	 */
	public Vector<RectangularPart> getVisibleParts()	{
		final RectangularPartCollection result = new RectangularPartCollection();
		for (final Window current : this.getWindowStack()) {
			result.add(current.getVisibleContext());
		}
		return result.toVector();
	}
	/**
	 * Creates a new {@link Window} and sets it as the top window.
	 */
	public void newWindow(){
		final Window newWindow = new Window();
//		for (final Window current : this.getWindowStack()) current.newTop(newWindow);
		this.setAsNewTopWindow(newWindow);
	}
	/**
	 * Sets the provided window as the top window.
	 * @param window is the new top window.
	 */
	public void setAsNewTopWindow(final Window window) {
		this.getWindowStack().remove(window);
		this.getWindowStack().add(0, window);
		for (final Window current : this.getWindowStack()) current.setAsTop(window);
	}
	/**
	 * Removes a window.
	 * @param window is the window to remove.
	 */
	public void dispose(final Window window) {
		window.dispose();
		this.getWindowStack().remove(window);
	}

}
